#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;

typedef struct TNode* PtrToTNode;
struct TNode {
    ElementType Data;
    PtrToTNode Left;
    PtrToTNode Right;
};
typedef PtrToTNode BinTree;

BinTree Find ( BinTree BST, ElementType X );
BinTree Find1 ( BinTree BST, ElementType X );
BinTree FindMin ( BinTree BST );
BinTree FindMax ( BinTree BST );
BinTree Insert ( BinTree BST, ElementType X );
BinTree Delete ( BinTree BST, ElementType X );

int main () {
    return 0;
}


BinTree Find ( BinTree BST, ElementType X ) {
    if ( !BST ) return NULL;
    if ( X > BST->Data ) return Find( BST->Right, X );
    else if ( X < BST->Data ) return Find( BST->Left, X );
    else return BST;    
}
BinTree Find1 ( BinTree BST, ElementType X ) {
    BinTree tmp = BST;
    while (BST) {
        if ( X > tmp->Data ) tmp = tmp->Right;
        else if ( X < tmp->Data ) tmp = tmp->Left;
        else break;
    }
    return tmp;
}
BinTree FindMin ( BinTree BST ) {
    if ( !BST ) return NULL;
    else if ( !BST->Left ) return BST;
    else return FindMin( BST->Left );
}
BinTree FindMax ( BinTree BST ) {
    if ( BST ) {
        while ( BST->Right ) BST = BST->Right;
    }
    return BST;
}
BinTree Insert ( BinTree BST, ElementType X ) {
    if ( !BST ) {
        BST = ( BinTree )malloc(sizeof(struct TNode));
        BST->Data = X;
        BST->Left = BST->Right = NULL;
    } else {
        if ( X > BST->Data ) {
            BST->Right = Insert ( BST->Right, X );
        } else if ( X < BST->Data ) {
            BST->Left = Insert ( BST->Left, X );
        } 
    }
    return BST;
}
BinTree Delete( BinTree BST, ElementType X ) {
    BinTree tmp;
    if ( !BST ) {
        printf("Not Found\n");
        // return NULL;
    } else {
        if ( BST->Data > X ) BST->Left = Delete(BST->Left,X);
        else if ( BST->Data < X ) BST->Right = Delete(BST->Right,X);
        else if ( BST->Data == X ) {
            if ( BST->Left && BST->Right ) {
                tmp = FindMin(BST->Right);
                BST->Data = tmp->Data;
                BST->Right = Delete(BST->Right, BST->Data);
            } else {
                tmp = BST;
                if ( !BST->Left ) BST = BST->Right;
                else BST = BST->Left;
                free(tmp);
            }
        }
    }
    return BST;
}